package com.cwnu.sw.cys.core.user.service.impl;

import com.cwnu.sw.cys.core.common.annotation.RedisLock;
import com.cwnu.sw.cys.core.common.domain.enums.IdempotentEnum;
import com.cwnu.sw.cys.core.common.domain.enums.YesOrNoEnum;
import com.cwnu.sw.cys.core.common.service.LockService;
import com.cwnu.sw.cys.core.user.dao.UserBackpackDao;
import com.cwnu.sw.cys.core.user.domain.entity.ItemConfig;
import com.cwnu.sw.cys.core.user.domain.entity.UserBackpack;
import com.cwnu.sw.cys.core.user.domain.enums.ItemTypeEnum;
import com.cwnu.sw.cys.core.user.service.UserBackpackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author jisam
 * @version 1.0
 * @contact "jisamchan@outlook.com"
 * @date 25/03/2025
 * @description
 */
@Service
public class UserBackpackServiceImpl implements UserBackpackService {
    @Autowired
    private LockService lockService;
    @Autowired
    private UserBackpackDao userBackpackDao;

    @Autowired
    @Lazy
    private UserBackpackServiceImpl userBackpackServiceImp;

    /**
     * @param uid
     * @param itemId
     * @param idempotentEnum
     * @param businessId
     */
    @Override
    public void acquireItem(Long uid, Long itemId, IdempotentEnum idempotentEnum, String businessId) {
        String idempotentKey = getIdempotentKey(itemId, idempotentEnum, businessId);
//        lockService.executeWithLock("acquireItem" + idempotentKey,() ->{
//        });
        userBackpackServiceImp.doAcquireItem(uid, itemId, idempotentKey);
    }

    @RedisLock(key = "#idempotentKey",waitTime = 3000)
    public void doAcquireItem(Long uid, Long itemId, String idempotentKey) {
        UserBackpack userBackpack = userBackpackDao.getByIdempotent(idempotentKey);
        //幂等检查
        if (Objects.nonNull(userBackpack)) {
            return;
        }
        //业务检查
        //ItemConfig itemConfig = itemCache.getById(itemId);
        //        if (ItemTypeEnum.BADGE.getType().equals(itemConfig.getType())) {//徽章类型做唯一性检查
        //            Integer countByValidItemId = userBackpackDao.getCountByValidItemId(uid, itemId);
        //            if (countByValidItemId > 0) {//已经有徽章了不发
        //                return;
        //            }
        //        }
        //发物品
        UserBackpack insert = UserBackpack.builder()
                .uid(uid)
                .itemId(itemId)
                .status(YesOrNoEnum.NO.getStatus())
                .idempotent(idempotentKey)
                .build();
        userBackpackDao.save(insert);
        //用户收到物品的事件
        //applicationEventPublisher.publishEvent(new ItemReceiveEvent(this, insert));
    }


    private String getIdempotentKey(Long itemId, IdempotentEnum idempotentEnum, String businessId) {
        return String.format("%d_%d_%s", itemId, idempotentEnum.getType(), businessId);
    }
}
